﻿namespace AgileZen.Models
{
    using System.Runtime.Serialization;
    using System;
    using System.Collections.Generic;

    [DataContract(Name="user")]
    public class Person : AgileZenModel
    {
        private int _id;
        private string _name;
        private string _bio;
        private string _username;
        private string _email;
        private List<Story> _stories;

        [DataMember(Name = "id", Order = 3)]
        public int Id 
        {
            get { return _id; }
            set 
            { 
                _id = value; 
                NotifyPropertyChanged("Id"); 
            } 
        }

        public bool ShouldSerializeId()
        {
            return PropertiesChanged.Contains("Id");
        }

        [DataMember(Name = "name", Order = 4)]
        public string Name
        {
            get { return _name; }
            set 
            { 
                _name = value; 
                NotifyPropertyChanged("Name"); 
            }
        }

        public bool ShouldSerializeName()
        {
            return PropertiesChanged.Contains("Name");
        }

        [DataMember(Name = "userName", Order = 5)]
        public string UserName
        {
            get { return _username; }
            set
            {
                _username = value;
                NotifyPropertyChanged("UserName");
            }
        }

        public bool ShouldSerializeUserName()
        {
            return PropertiesChanged.Contains("UserName");
        }

        [DataMember(Name = "bio", Order = 0)]
        public string Bio
        {
            get { return _bio; }
            set
            {
                _bio = value;
                NotifyPropertyChanged("Bio");
            }
        }

        public bool ShouldSerializeBio()
        {
            return PropertiesChanged.Contains("Bio");
        }

        [DataMember(Name = "email", Order = 2)]
        public string Email
        {
            get { return _email; }
            set
            {
                _email = value;
                NotifyPropertyChanged("Email");
            }
        }

        public bool ShouldSerializeEmail()
        {
            return PropertiesChanged.Contains("Email");
        }

        //catch date first in a string, because the JSON serializer expects a Date(*) notation
        [DataMember(Name = "createTime", Order = 1)]
        private string CreatedOnUTCString { get; set; }
        
        [Obsolete]
        public DateTime? CreatedOn
        {
            get
            {
                bool parsed = false;
                DateTime datetime = DateTime.MinValue;
                if (!String.IsNullOrEmpty(CreatedOnUTCString))
                {
                    parsed = DateTime.TryParse(CreatedOnUTCString, out datetime);
                }

                return (parsed) ? datetime : new Nullable<DateTime>();
            }
        }

        [DataMember(Name = "stories", Order = 6)]
        public List<Story> Stories 
        {
            get { return _stories; }
            set
            {
                _stories = value;
                NotifyPropertyChanged("Stories");
            }

        }

        public bool ShouldSerializeStories()
        {
            return PropertiesChanged.Contains("Stories");
        }


        public static class Enrichments
        {
            public const string Stories = "stories";
        }
    }
}
